package com.ym.student.system.controller;

import com.ym.student.system.entity.SysMenu;
import com.ym.student.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/menus")
public class SysMenuController {

    @Autowired
    private ISysMenuService sysMenuService;

    // 获取所有菜单
    @GetMapping
    public List<SysMenu> getAllMenus() {
        return sysMenuService.list();
    }

    // 根据ID获取菜单
    @GetMapping("/{id}")
    public SysMenu getMenuById(@PathVariable("id") Integer menuId) {
        return sysMenuService.getById(menuId);
    }

    // 添加菜单
    @PostMapping
    public boolean addMenu(@RequestBody SysMenu sysMenu) {
        return sysMenuService.save(sysMenu);
    }

    // 更新菜单
    @PutMapping("/{id}")
    public boolean updateMenu(@PathVariable("id") Integer menuId, @RequestBody SysMenu sysMenu) {
        sysMenu.setMenuId(menuId); // 确保更新的是指定ID的菜单
        return sysMenuService.updateById(sysMenu);
    }

    // 删除菜单
    @DeleteMapping("/{id}")
    public boolean deleteMenu(@PathVariable("id") Integer menuId) {
        return sysMenuService.removeById(menuId);
    }

}